/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Invoice2.java
 *
 * Created on 21-08-2011, 12:44:01
 */
package tuitionmanagement.gui;

import Business.InvoiceBuss;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import tuitionmanagement.DAO.InvoiceDao;
import tuitionmanagement.entity.InvoiceEntity;

/**
 *
 * @author XUAN
 */
public class Invoice extends javax.swing.JPanel {

    InvoiceEntity invoice = new InvoiceEntity();
    InvoiceBuss inBuss = new InvoiceBuss();
    InvoiceDao indao = new InvoiceDao();
    ArrayList<InvoiceEntity> lst;
    int index;
    Vector vRow = new Vector();
    public int confirm;
    private String lastError;

    /** Creates new form Invoice2 */
    public Invoice() {
        initComponents();
        lst = inBuss.readByAll();
        loadTableInvoiceDetail(index);
    }

    private void loadTableInvoiceList(ArrayList<InvoiceEntity> lst) {
        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();
            invoice = lst.get(i);
            v.addElement(invoice.getInvoiceId());
            v.addElement(invoice.getStudentName());
            v.addElement(invoice.getClassTypeName());
            v.addElement(invoice.getFeeLevelId());

            vRow.addElement(v);
        }

        Vector vCol = new Vector();

        vCol.addElement("InvoiceId");
        vCol.addElement("StudentName");
        vCol.addElement("ClassTypeName");
        vCol.addElement("FeeLevelId");

        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tbInvoice.setModel(tblModel);
    }

    private void loadAllInvoice() {
        lst.clear();
        lst = inBuss.readByAll();
        loadTableInvoiceList(lst);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtStudentName = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtFeeLevelId = new javax.swing.JTextField();
        txtInvoiceid = new javax.swing.JTextField();
        txtClassTypeName = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbInvoice = new javax.swing.JTable();

        setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setForeground(new java.awt.Color(51, 0, 255));
        jLabel1.setText("Invoice");
        add(jLabel1, new java.awt.GridBagConstraints());

        jPanel2.setLayout(new java.awt.GridBagLayout());

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("InvoiceId:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel7, gridBagConstraints);

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel9.setText("Class Type Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel9, gridBagConstraints);

        txtStudentName.setMinimumSize(new java.awt.Dimension(150, 20));
        txtStudentName.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtStudentName, gridBagConstraints);

        jLabel10.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel10.setText("FeeLevelId:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 1, 5);
        jPanel2.add(jLabel10, gridBagConstraints);

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Student Name:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel2.add(jLabel8, gridBagConstraints);

        txtFeeLevelId.setMinimumSize(new java.awt.Dimension(150, 20));
        txtFeeLevelId.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 1, 5);
        jPanel2.add(txtFeeLevelId, gridBagConstraints);

        txtInvoiceid.setMinimumSize(new java.awt.Dimension(150, 20));
        txtInvoiceid.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtInvoiceid, gridBagConstraints);

        txtClassTypeName.setMinimumSize(new java.awt.Dimension(150, 20));
        txtClassTypeName.setPreferredSize(new java.awt.Dimension(150, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 150;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 5, 5);
        jPanel2.add(txtClassTypeName, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        add(jPanel2, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(btnAdd, gridBagConstraints);

        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(btnEdit, gridBagConstraints);

        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        jPanel3.add(btnDelete, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        add(jPanel3, gridBagConstraints);

        jPanel4.setLayout(new java.awt.GridBagLayout());

        tbInvoice.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        tbInvoice.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        tbInvoice.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "InvoiceId", "StudentName", "ClassTypeName", "FeeLevelName"
            }
        ));
        tbInvoice.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbInvoiceMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbInvoice);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 400;
        gridBagConstraints.ipady = 100;
        jPanel4.add(jScrollPane1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 10, 5);
        add(jPanel4, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void loadTableInvoiceDetail(int index) {
        ArrayList<InvoiceEntity> list = inBuss.readByAll();
        for (int i = 0; i < list.size(); i++) {
            if (index != list.get(i).getInvoiceId()) {
                list.remove(i);
                i--;
            }
        }
        loadAllInvoice();
    }

    private void tbInvoiceMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbInvoiceMouseClicked
        // TODO add your handling code here:
        int selectedIndex = tbInvoice.getSelectedRow();
        if (selectedIndex > -1) {
            // RateEntity rate = lst.get(selectedIndex);
            txtInvoiceid.setText(String.valueOf(lst.get(selectedIndex).getInvoiceId()));
            txtStudentName.setText(String.valueOf(lst.get(selectedIndex).getStudentName()));
            txtClassTypeName.setText(lst.get(selectedIndex).getClassTypeName());
            txtFeeLevelId.setText(String.valueOf(lst.get(selectedIndex).getFeeLevelId()));

        }
    }//GEN-LAST:event_tbInvoiceMouseClicked

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        if (txtClassTypeName.getText().trim().equals("") || txtFeeLevelId.getText().trim().equals("") || txtInvoiceid.getText().trim().equals("") || txtStudentName.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }
        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New Invoice", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {

            invoice.setInvoiceId(Integer.parseInt(txtInvoiceid.getText()));
            invoice.setStudentName(String.valueOf(txtStudentName.getText()));
            invoice.setClassTypeName(String.valueOf(txtClassTypeName.getText()));
            invoice.setFeeLevelId(Integer.parseInt(txtFeeLevelId.getText()));

//            if (inBuss.addInvoice(invoice)) {
//                JOptionPane.showMessageDialog(this, inBuss.getLastError());
//
//                txtInvoiceid.setText(null);
//                txtStudentName.setText(null);
//                txtClassTypeName.setText(null);
//                txtFeeLevelId.setText(null);
//
//                txtInvoiceid.requestFocus();
//                vRow.clear();
//                loadAllInvoice();
//                this.tbInvoice.updateUI();
//            } else {
//                JOptionPane.showMessageDialog(this, inBuss.getLastError());
//            }
        } else {

            txtInvoiceid.setText(null);
            txtStudentName.setText(null);
            txtClassTypeName.setText(null);
            txtFeeLevelId.setText(null);
           
        }
    }//GEN-LAST:event_btnAddActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        int selectedIndex = tbInvoice.getSelectedRow();
        InvoiceEntity or = lst.get(selectedIndex);
        int invoiceId = or.getInvoiceId();
        if (txtClassTypeName.getText().trim().equals("") || txtFeeLevelId.getText().trim().equals("") || txtInvoiceid.getText().trim().equals("") || txtStudentName.getText().trim().equals("")) {
            JOptionPane.showMessageDialog(this, "Please fill in all the blanks!");
            return;
        }
        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update Invoice", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {

            or.setInvoiceId(Integer.parseInt(txtInvoiceid.getText()));
            or.setStudentName(String.valueOf(txtStudentName.getText()));
            or.setClassTypeName(String.valueOf(txtClassTypeName.getText()));
            or.setFeeLevelId(Integer.parseInt(txtFeeLevelId.getText()));

            if (inBuss.update(invoiceId)) {
                //note
                JOptionPane.showMessageDialog(this, or.getLastError());
                vRow.clear();
                loadAllInvoice();
                this.tbInvoice.updateUI();
                txtInvoiceid.setText(null);
                txtStudentName.setText(null);
                txtClassTypeName.setText(null);
                txtFeeLevelId.setText(null);

            } else {
                JOptionPane.showMessageDialog(this, or.getLastError());
            }
        }

    }//GEN-LAST:event_btnEditActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        int selectedIndex = tbInvoice.getSelectedRow();
        InvoiceEntity or = lst.get(selectedIndex);
        int invoiceId = or.getInvoiceId();

        confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Order", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
        if (confirm == JOptionPane.YES_OPTION) {
            if (inBuss.delete(invoiceId)) {
                JOptionPane.showMessageDialog(this, inBuss.getLastError());
                txtInvoiceid.setText(null);
                txtClassTypeName.setText(null);
                txtStudentName.setText(null);
                txtFeeLevelId.setText(null);
                vRow.clear();
                loadAllInvoice();
                this.tbInvoice.updateUI();
            } else {
                JOptionPane.showMessageDialog(this, inBuss.getLastError());
            }
        }

    }//GEN-LAST:event_btnDeleteActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbInvoice;
    private javax.swing.JTextField txtClassTypeName;
    private javax.swing.JTextField txtFeeLevelId;
    private javax.swing.JTextField txtInvoiceid;
    private javax.swing.JTextField txtStudentName;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
